package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.report.util.DetailReportModel;
import com.codeoss.framework.report.util.DetailReportUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.report.DetailReport;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.impl.IrReportService;

@Service("detailreportRenderer")
public class DetailReportRenderer extends CommonComponentRenderer {
	@Autowired
	private IrReportService irReportService;
	@Autowired
	private DetailReportUtil detailReportUtil;
	@Override
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		DetailReport detailReport=(DetailReport)component;
		viewSerializeMap.put("reportCode", detailReport.getReportCode());
		
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", detailReport.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		if(irReport!=null) {
			viewSerializeMap.put("title", irReport.getName());
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext,
			Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap=super.startRenderProp(yunContext, component, viewPropMap);
		DetailReportModel detailReportModel=yunContext.getAttribute(component.getId()+"_reportdata");
		if(detailReportModel==null) {
			DetailReport detailReport=(DetailReport)component;
			QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
			query.eq("code", detailReport.getReportCode());
			IrReport irReport=irReportService.getOne(query);
			String jsonContent=irReport.getJsonContent();
			com.codeoss.framework.report.def.DetailReport report=null;
			if(!StringUtils.isEmpty(jsonContent)) {
				report=detailReportUtil.buildDetailReportFromIrReport(irReport);
			}
			detailReportModel=detailReportUtil.buildDetailReportData(report);
		}
		propSerializeMap.put("detailReportModel", detailReportModel);
		return propSerializeMap;
	}
}
